﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class index : webBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["cat"] != null) {
            Category.Visible = false;
            Argoment.Visible = true;
            Topic.Visible = false;
        }
        if (Request.QueryString["topic"] != null)
        {
            Category.Visible = false;
            Argoment.Visible = false;
            Topic.Visible = true;
        }
    }

    public void loadCategory()
    {

        List<String> categorie = ForumCore.returnCategory();
        int count = 0;

        Response.ContentType = "text/html";
        Response.Write("<div class=\"datagrid\">");
        Response.Write("<table>");
        Response.Write("<thead>");
        Response.Write("<tr>");
        Response.Write("<th>CATEGORIE</th>");
        Response.Write("<th>STATISTICHE</th>");
        Response.Write("<th>Creatore</th>");
        Response.Write("</tr>");
        Response.Write("</thead>");
        Response.Write("<tbody>");
        foreach (String s in categorie)
        {
            count++;
            if (count % 2 != 0)
            {
                Response.Write("<tr>");
                Response.Write("<td><a href=\"?cat=" + s + "\">" + s + "</a></td>");
                Response.Write("<td>Topic: " + ForumCore.getNrTopic(s) + "</td>");
                Response.Write("<td>"+ ForumCore.returnCategoryOwner(s)+ "</td>");
                Response.Write("</tr>");
            }
            else
            {
                Response.Write("<tr class=\"alt\">");
                Response.Write("<td><a href=\"?cat=" + s + "\">" + s + "</a></td>");
                Response.Write("<td>Topic: " + ForumCore.getNrTopic(s) + "</td>");
                Response.Write("<td>" + ForumCore.returnCategoryOwner(s) + "</td>");
                Response.Write("</tr>");
            }
        }
        if (categorie.Count == 0)
        {
            Response.Write("<tr>");
            Response.Write("<td>Non ci sono categorie</td>");
            Response.Write("</tr>");
        }
        Response.Write("</tbody>");
        Response.Write("</table>");
        Response.Write("</div>");
        Response.Write("<br/>");
        Response.Write("<br/>");
        Response.Write("<br/>");



    }
    public void loadArgoment(String scelta) {

        Response.Write("<br/>");
        Response.Write("<br/>");

        List<String> topic = ForumCore.returnArgoment(scelta);
        List<String> creatore = ForumCore.returnArgomentOwner(scelta);
        int count = 0;
        int i = 0;

        Response.ContentType = "text/html";
        Response.Write("<div class=\"datagrid\">");
        Response.Write("<table>");
        Response.Write("<thead>");
        Response.Write("<tr>");
        Response.Write("<th>TOPIC</th>");
        Response.Write("<th>STATISTICHE</th>");
        Response.Write("<th>Creatore</th>");
        Response.Write("</tr>");
        Response.Write("</thead>");
        Response.Write("<tbody>");
        foreach (String s in topic)
        {
            count++;
            if (count % 2 != 0)
            {
                Response.Write("<tr>");
                Response.Write("<td><a href=\"?cat=" + Request.QueryString["cat"] + "&topic=" + s + "\">" + s + "</a></td>");
                Response.Write("<td>Nr Post: "+ ForumCore.getNrPost(s) + "</td>");
                Response.Write("<td>"+creatore[i]+"</td>");
                Response.Write("</tr>");
            }
            else
            {
                Response.Write("<tr class=\"alt\">");
                Response.Write("<td><a href=\"?cat=" + Request.QueryString["cat"] + "&topic=" + s + "\">" + s + "</a></td>");
                Response.Write("<td>Nr Post: " + ForumCore.getNrPost(s) + "</td>");
                Response.Write("<td>" + creatore [i]+ "</td>");
                Response.Write("</tr>");
            }
            i++;
        }
        if (topic.Count == 0)
        {
            Response.Write("<tr>");
            Response.Write("<td>Non ci sono topic per questa categoria</td>");
            Response.Write("</tr>");
        }
        Response.Write("</tbody>");
        Response.Write("</table>");
        Response.Write("</div>");
        Response.Write("<br/>");
        Response.Write("<br/>");
        Response.Write("<br/>");

        
        
    }
    public void loadPost(String scelta)
    {

        Response.Write("<br/>");
        Response.Write("<br/>");

        List<String> autore = ForumCore.returnPostOwner(scelta);
        List<String> post = ForumCore.returnPost(scelta);
        int i = 0;

        Response.ContentType = "text/html";
        Response.Write("<div class=\"datagrid\">");
        Response.Write("<table>");
        Response.Write("<thead>");
        Response.Write("<tr>");
        Response.Write("<th>" + scelta + "</th>");
        Response.Write("</tr>");
        Response.Write("</thead>");
        Response.Write("<tbody>");
        foreach (String s in post)
        {
            Response.Write("<tr style=\"display: block; border-bottom: 1px solid #F7CDCD;\">");
            Response.Write("<td style=\" height:150px; width: 300px; text-align: center;\" ><img src=\"" + utenteCore.imgUsr(autore[i]) + "\" style=\" display: block; width: 80px; margin: 0 auto; \"> Nome:  <font color=\"green\">" + autore[i] + "</font></td>");
                Response.Write("<td>"+ s + "</td>");
                Response.Write("</tr>");
                i++;
        }
        if (post.Count == 0)
        {
            Response.Write("<tr>");
            Response.Write("<td>Non ci sono Post per questo topic</td>");
            Response.Write("</tr>");
        }
        Response.Write("</tbody>");
        Response.Write("</table>");
        Response.Write("</div>");



    }
    protected void addTopic_Click(object sender, EventArgs e)
    {
        AggiungiTopic.Visible = true;
        addTopic.Visible = false;

    }
    protected void sendAddTopic_Click(object sender, EventArgs e)
    {
        DB.insertWithOutPar("ForumDB", "insert into topic(name, creator, visible, category) values ('" + nomeTopic.Text + "' ,'" + utente.username + "',1,'" + Request.QueryString["cat"] + "')");
    }


    protected void BInviaTxt_Click(object sender, EventArgs e)
    {

        DB.insertWithOutPar("ForumDB","insert into post(content,creator,topic_id) values ('"+TBtesto.Text+"','"+utente.username+"', (select id_topic from topic where name ='" +Request.QueryString["topic"]+ "'))");
        TBtesto.Text = "";

    }
}